package com.ruoyi.sale.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.sale.domain.*;
import com.ruoyi.sale.mapper.*;
import com.ruoyi.sale.vo.SaleReturnVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.service.ISaleReturnService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售退货管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-03
 */
@Service
public class SaleReturnServiceImpl implements ISaleReturnService 
{
    @Autowired
    private SaleReturnMapper saleReturnMapper;
    @Autowired
    private SaleStoreoutMapper saleStoreoutMapper;
    @Autowired
    private SysAccountMapper accountMapper;
    @Autowired
    private SysCustomerMapper customerMapper;
    @Autowired
    private SaleStoreoutDetailMapper saleStoreoutDetailMapper;
    @Autowired
    private SalePayorderMapper salePayorderMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;
    @Autowired
    private SysStoreMapper sysStoreMapper;
    @Autowired
    private SaleReturnDetailMapper saleReturnDetailMapper;

    /**
     * 查询销售退货管理
     * 
     * @param id 销售退货管理主键
     * @return 销售退货管理
     */
    @Override
    public SaleReturn selectSaleReturnById(Long id)
    {
        return saleReturnMapper.selectSaleReturnById(id);
    }

    /**
     * 查询销售退货管理列表
     * 
     * @param saleReturn 销售退货管理
     * @return 销售退货管理
     */
    @Override
    public List<SaleReturn> selectSaleReturnList(SaleReturn saleReturn)
    {
        return saleReturnMapper.selectSaleReturnList(saleReturn);
    }

    /**
     * @description: 新增销售退款单
     * @param: saleReturnVo
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @author clqlongbao
     * @date: 2022/9/3 14:08
     */
    @Override
    @Transactional
    public int insertSaleReturn(SaleReturnVo saleReturnVo){
        Date date = new Date();
        //获取当前登陆人
        LoginUser user = SecurityUtils.getLoginUser();
        System.out.println(user.toString());
        //退货单和出库单使用同一个订单号
        Long orderNo = saleReturnVo.getSelectStoreoutList().get(0).getOrderNo();
        System.out.println("销售订单号为："+orderNo);
        //保存退货主订单
        SaleReturn saleReturn = new SaleReturn();
        BeanUtils.copyProperties(saleReturnVo,saleReturn);
        saleReturn.setOrderNo(orderNo);
        saleReturn.setCreateBy(user.getUsername());
        saleReturn.setCreateTime(date);
        //结算账户
        SysAccount account = accountMapper.selectSysAccountById(saleReturnVo.getAccountId());
        saleReturn.setAccountName(account.getName());
        saleReturn.setCustomerName(customerMapper.selectSysCustomerById(saleReturnVo.getCustomerId()).getName());
        List<SaleStoreout> storeoutList = saleReturnVo.getSelectStoreoutList();
        String productName = storeoutList.stream().map(SaleStoreout::getProductName).collect(Collectors.toList()).toString();
        System.out.println("商品名称："+productName);
        saleReturn.setProductName(productName);
        saleReturnMapper.insertSaleReturn(saleReturn);
        System.out.println("退货主订单保存成功");
        //保存退货子订单
        //根据订单号查询退货的商品详情
        List<SaleOrderDetail> saleOrderDetails = saleOrderDetailMapper.selectSaleOrderdetailByOrderNo(orderNo);
        saleOrderDetails.forEach(saleOrderDetail->{
            SaleReturnDetail saleReturnDetail = new SaleReturnDetail();
            BeanUtils.copyProperties(saleOrderDetail,saleReturnDetail);
            saleReturnDetail.setId(null);
            saleReturnDetail.setOrderNo(orderNo);
            saleReturnDetail.setCreateBy(user.getUsername());
            saleReturnDetail.setCreateTime(date);
            saleReturnDetailMapper.insertSaleReturnDetail(saleReturnDetail);
        });
        System.out.println("退货明细订单保存成功");
        //封装支付单
        SalePayorder salePayorder = new SalePayorder();
        salePayorder.setOrderNo(orderNo);
        salePayorder.setAccountId(saleReturnVo.getAccountId());
        salePayorder.setProductName(productName);
        salePayorder.setAccountName(account.getName());
        salePayorder.setAmount(saleReturnVo.getAmount());
        salePayorder.setEarnest(saleReturnVo.getEarnest());
        salePayorder.setPay(saleReturnVo.getPay());
        salePayorder.setReduced(saleReturnVo.getReduced());
        salePayorder.setUserId(user.getUserid());
        salePayorder.setCreateTime(date);
        salePayorderMapper.insertSalePayorder(salePayorder);
        System.out.println("退货支付单保存成功");
        //模拟支付
        account.setBalance(account.getBalance()-saleReturnVo.getPay());
        account.setUpdateTime(date);
        accountMapper.updateSysAccount(account);
        System.out.println("账户增加退款金额成功");
        //增加库存
        //得到该商品的库存信息
        saleOrderDetails.forEach(saleOrderDetail->{
            //得到该商品的库存信息
            SysStore sysStore=sysStoreMapper.selectSysStoreByproductName(saleOrderDetail.getProductName());
            //增加库存
            Integer integer = Integer.valueOf(sysStore.getNumber());//原始库存
            int i = integer + saleOrderDetail.getNumber();
            sysStore.setNumber(Integer.valueOf(i).toString());
            sysStore.setUpdateBy(user.getUsername());
            sysStore.setUpdateTime(date);
            sysStoreMapper.updateSysStore(sysStore);
        });
        System.out.println("退款库存增加成功");
        return 1;
    }

    /**
     * 修改销售退货管理
     * 
     * @param saleReturn 销售退货管理
     * @return 结果
     */
    @Override
    public int updateSaleReturn(SaleReturn saleReturn)
    {
        saleReturn.setUpdateTime(DateUtils.getNowDate());
        return saleReturnMapper.updateSaleReturn(saleReturn);
    }

    /**
     * 批量删除销售退货管理
     * 
     * @param ids 需要删除的销售退货管理主键
     * @return 结果
     */
    @Override
    public int deleteSaleReturnByIds(Long[] ids)
    {
        return saleReturnMapper.deleteSaleReturnByIds(ids);
    }

    /**
     * 删除销售退货管理信息
     * 
     * @param id 销售退货管理主键
     * @return 结果
     */
    @Override
    public int deleteSaleReturnById(Long id)
    {
        return saleReturnMapper.deleteSaleReturnById(id);
    }
}
